package com.arsl.client.view;

import java.util.Date;

import com.arsl.client.GlobalInfo;
import com.arsl.client.PaperaziServiceAsync;
import com.arsl.client.data.Edition;
import com.arsl.client.data.Paper;
import com.arsl.client.data.Section;
import com.arsl.client.util.Constants;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

public class PageSelectorWidget extends Composite {

	int paperId = 0;
	java.util.Date date;
	int pageNo = 0;

	PaperaziServiceAsync rpcService;

	@UiField
	ListBox lstPapers;

	@UiField
	ListBox lstEditions;

	@UiField
	ListBox lstSections;

	@UiField
	DateBox dtDate;

	@UiField
	TextBox txtPage;
	private int editionId;
	private int sectionId;

	@UiHandler("lstPapers")
	public void onSelectPaper(ChangeEvent event) {
		updateSectionsEditions();
	}

	private void updateSectionsEditions() {
		final int paperId = getPaperId();
		rpcService.getEditions(paperId, new AsyncCallback<Edition[]>() {

			@Override
			public void onSuccess(Edition[] result) {
				setEditions(result);
				rpcService.getSections(paperId, new AsyncCallback<Section[]>() {

					@Override
					public void onSuccess(Section[] result) {
						setSections(result);
					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						caught.printStackTrace();
					}
				});
			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		});

	}

	public void populatePapers() {
		rpcService.getPapers(GlobalInfo.publisherId,
				new AsyncCallback<Paper[]>() {

					@Override
					public void onSuccess(Paper[] result) {
						setPapers(result);
					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}
				});
	}

	private static PageSelectorWidgetUiBinder uiBinder = GWT
			.create(PageSelectorWidgetUiBinder.class);

	interface PageSelectorWidgetUiBinder extends
			UiBinder<Widget, PageSelectorWidget> {
	}

	public PageSelectorWidget() {
		initWidget(uiBinder.createAndBindUi(this));
		txtPage.setText("1");
		// dtDate.setValue(new Date());

		Date now = new Date();
		now.setTime(now.getTime() + Constants.DAY_IN_MILIS);
		dtDate.setValue(now);

		dtDate.setFormat((new DateBox.DefaultFormat(DateTimeFormat
				.getFormat(PredefinedFormat.DATE_SHORT))));

	}

	public void setRpcService(PaperaziServiceAsync rpcService) {
		this.rpcService = rpcService;

	}

	public int getPaperId() {
		int selIndex = lstPapers.getSelectedIndex();
		paperId = Integer.parseInt(lstPapers.getValue(selIndex));
		return paperId;
	}

	public int getEditionId() {
		int selIndex = lstEditions.getSelectedIndex();
		editionId = Integer.parseInt(lstEditions.getValue(selIndex));
		return editionId;
	}

	public int getSectionId() {
		int selIndex = lstSections.getSelectedIndex();
		sectionId = Integer.parseInt(lstSections.getValue(selIndex));
		return sectionId;
	}

	public void setPaperId(int paperId) {
		this.paperId = paperId;

	}

	public boolean isValid() {
		if (getDate() == null || getPageNo() <= 0 || getPaperId() == 0) {
			return false;
		}
		return true;
	}

	public Date getDate() {
		date = dtDate.getValue();
		return date;
	}

	public void setPapers(Paper[] papers) {
		lstPapers.clear();
		for (int i = 0; i < papers.length; i++) {
			lstPapers.addItem(papers[i].getName(),
					String.valueOf(papers[i].getId()));
		}
		updateSectionsEditions();
	}

	public void setEditions(Edition[] editions) {
		lstEditions.clear();
		for (int i = 0; i < editions.length; i++) {
			lstEditions.addItem(editions[i].getName(),
					String.valueOf(editions[i].getId()));
		}
	}

	public void setSections(Section[] sections) {
		lstSections.clear();
		for (int i = 0; i < sections.length; i++) {
			lstSections.addItem(sections[i].getName(),
					String.valueOf(sections[i].getId()));
		}
	}

	public void setDate(Date date) {
		this.date = date;
		dtDate.setValue(date);
	}

	public int getPageNo() {
		try {
			this.pageNo = Integer.parseInt(txtPage.getText());
		} catch (NumberFormatException e) {
			return 0;
		}
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
		txtPage.setText(Integer.toString(pageNo));
	}

}
